/**
 * 
 */
package com.wgo.precise.client.ui.view.editor.requirementtable;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

import com.wgo.precise.client.ui.controller.ModelStatus;
import com.wgo.precise.client.ui.controller.RequirementPlugin;
import com.wgo.precise.client.ui.controller.listeners.ModelItemDeletedListener;
import com.wgo.precise.client.ui.controller.listeners.NewModelItemCreatedListener;
import com.wgo.precise.client.ui.controller.listeners.SessionStatusListener;
import com.wgo.precise.client.ui.model.RequirementWrapper;
import com.wgo.precise.client.ui.model.facade.IModelPersistable;
import com.wgo.precise.client.ui.model.facade.INodeParent;
import com.wgo.precise.client.ui.view.editor.AbstractFormPage;


/**
 * @author PEide3
 */
public class RequirementTableFormPage extends AbstractFormPage implements NewModelItemCreatedListener, ModelItemDeletedListener, SessionStatusListener {

	public final static String PAGE_ID = "requriementsTable" ;
	public final static String PAGE_TITLE = "Requirements" ;

	public final static int REQ_TABLE_STYLE = /*SWT.VIRTUAL |*/ SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION ;
	public final static int REQ_TABLE_GRID_STYLE = GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING ;
	private TableViewer viewer = null ;
	
	public RequirementTableFormPage(FormEditor editor) {
		super(editor, PAGE_ID, PAGE_TITLE);
	}

	/**
	 * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
	 */
	@Override
	protected void createFormContent(IManagedForm managedForm) {
		super.createFormContent(managedForm);
		updateText();
		
		FormToolkit toolkit = managedForm.getToolkit(); 
		ScrolledForm form = managedForm.getForm();

		form.setText("Requirements");
		form.setToolTipText("List of requirements of this category.");
		
		Composite body = form.getBody();
		
		GridLayout layout = new GridLayout();
		layout.marginWidth = 10;
		layout.marginHeight = 10;

		body.setLayout(layout);
		
		
		Table table = toolkit.createTable(body, REQ_TABLE_STYLE );
		TableLayout tlayout = new TableLayout() ;

//		TableColumn column = null;
//		int tableWithHint = 10;
//		for ( HistoryColumnProperties prop : HistoryColumnProperties.values()) {
//			column = new TableColumn (table, SWT.LEFT, prop.ordinal());
//			column.setText(prop.name());
//			tlayout.addColumnData(new ColumnPixelData(prop.width()));
//			tableWithHint += prop.width() ;
//		}

		GridData gd = new GridData();
		gd = new GridData( REQ_TABLE_GRID_STYLE );
		gd.minimumHeight = 60 ;

		table.setLayoutData(gd);
		table.setLayout(tlayout);
		
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
		
		viewer = new TableViewer( table );
		viewer.setContentProvider(new TableContentProvider());
//		viewer.setColumnProperties(HistoryColumnProperties.names());
//		viewer.setLabelProvider(new HistoryTableLabelProvider());
		//viewer.setSorter(new TableColumnSorter());
		
		
		viewer.setInput(getModelItem());

//		IModelFormPart = 
//		formParts = ((ClientModelItem)getModelItem()).getPropertiesFormParts(this, getManagedForm());
//		Composite previous = null;
//		FillLayout layout = null;
//		for (IModelFormPart part : formParts) {
////			FormData data = new FormData();
////			data.left = new FormAttachment(0,5);
////			if (null == previous) {
////				data.top = new FormAttachment(0,0);
////			} else {
////				data.top = new FormAttachment(previous, 10);
////			}
////			data.right = new FormAttachment(100, -5);
//			previous = part.getSection().getParent();
////			previous.setLayoutData(data);
//			layout = new FillLayout();
//			previous.setLayout(layout);
//			getManagedForm().addPart(part);
//		}
//		managedForm.reflow(true);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.editor.FormPage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
		viewer = null ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.editor.FormPage#setActive(boolean)
	 */
	@Override
	public void setActive(boolean active) {
//		if (active) { //-no need due to refresh is triggered by ModelEditorClass
//			refresh();
//		}
		super.setActive(active);
	}

	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.view.editor.ModelFormPage#refresh()
	 */
	@Override
	public synchronized void refresh() {
		RequirementPlugin.log(new ModelStatus (IStatus.INFO , RequirementTableFormPage.class.getSimpleName() + " update.."  ) );
		viewer.setInput(getModelItem());
		viewer.refresh(true);
		getManagedForm().reflow(true);
	}

	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.controller.listeners.NewModelItemCreatedListener#onNewModelItemCreated(com.wgo.precise.client.ui.model.facade.IModelPersistable)
	 */
	public void onNewModelItemCreated(IModelPersistable createdItem) {
		RequirementPlugin.log(new ModelStatus (IStatus.INFO , RequirementTableFormPage.class.getSimpleName() + " item created.. is active: " + isActive()  ) );
		if (isActive() && createdItem instanceof RequirementWrapper && ((RequirementWrapper)createdItem).getParent().equals(getModelItem())){
			viewer.add(createdItem);
		}
	}

	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.controller.listeners.ModelItemDeletedListener#onModelItemDeleted(com.wgo.precise.client.ui.model.facade.IModelPersistable)
	 */
	public void onModelItemDeleted(IModelPersistable deletedItem, INodeParent parent) {
		if (isActive() && deletedItem instanceof RequirementWrapper && parent.equals(getModelItem())){
			viewer.remove(deletedItem);
		}
	}

	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.controller.listeners.SessionStatusListener#onSave()
	 */
	public void onSave() {
		if ( isActive() ) {
			viewer.refresh(null,true);
		}
	}

	
	
	
}
